﻿
Public Class undefined
    Private Const _name As String = "u"

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        var.invoke(inlineScript, _name, "undefined")
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal obj As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        actionTarget += obj + "==" + name(inlineScript)
        Return actionTarget
    End Function

    Public Shared Function name(ByVal inlineScript As inlineScript, _
                                Optional ByRef actionTarget As String = Nothing) As String
        If Not inlineScript Is Nothing Then
            register(inlineScript)
        End If

        actionTarget += _name
        Return actionTarget
    End Function
End Class

Public Class defined
    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal obj As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        actionTarget += obj + "!=" + undefined.name(inlineScript)
        Return actionTarget
    End Function
End Class

Public Class [true]
    Private Const _name As String = "t"

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        var.invoke(inlineScript, _name, "true")
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal obj As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        actionTarget += obj + "==" + name(inlineScript)
        Return actionTarget
    End Function

    Public Shared Function name(ByVal inlineScript As inlineScript, _
                                Optional ByRef actionTarget As String = Nothing) As String
        If Not inlineScript Is Nothing Then
            register(inlineScript)
        End If

        actionTarget += _name
        Return actionTarget
    End Function
End Class

Public Class [false]
    Private Const _name As String = "f"

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        var.invoke(inlineScript, _name, "false")
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal obj As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        actionTarget += obj + "==" + name(inlineScript)
        Return actionTarget
    End Function

    Public Shared Function name(ByVal inlineScript As inlineScript, _
                                Optional ByRef actionTarget As String = Nothing) As String
        If Not inlineScript Is Nothing Then
            register(inlineScript)
        End If

        actionTarget += _name
        Return actionTarget
    End Function
End Class

Public Class null
    Private Const _name As String = "n"

    Public Shared Sub register(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        var.invoke(inlineScript, _name, "null")
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal obj As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        actionTarget += obj + "==" + name(inlineScript)
        Return actionTarget
    End Function

    Public Shared Function name(ByVal inlineScript As inlineScript, _
                                Optional ByRef actionTarget As String = Nothing) As String
        If Not inlineScript Is Nothing Then
            register(inlineScript)
        End If

        actionTarget += _name
        Return actionTarget
    End Function
End Class

Public Class notnull
    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal obj As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        actionTarget += obj + "!=" + null.name(inlineScript)
        Return actionTarget
    End Function
End Class

Public Class eval
    Public Shared Function invoke(ByVal input As String, Optional ByRef actionTarget As String = Nothing) As String
        actionTarget += "eval(" + input + ")"
        Return actionTarget
    End Function
End Class

Public Class [typeof]
    Public Shared Function invoke(ByVal input As String, Optional ByRef actionTarget As String = Nothing) As String
        actionTarget += "typeof(" + input + ")"
        Return actionTarget
    End Function
End Class

Public Class isfunction
    Public Shared Function invoke(ByVal input As String, Optional ByRef actionTarget As String = Nothing) As String
        actionTarget += [typeof].invoke(eval.invoke(input)) + "=='function'"
        Return actionTarget
    End Function
End Class

Public Class existfunction
    Public Const functionName As String = "extfun"

    Public Shared Function invoke(ByVal inlineScript As inlineScript, ByVal name As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        inlineScript.addscript("existfunction", "function " + functionName + "(i){" + _
                               trycatch.invoke("return " + isfunction.invoke("i"), "return false") + "}")
        actionTarget += functionName + "(" + name + ")"

        Return actionTarget
    End Function

    Public Shared Function invoke(ByVal page As xhtmlpage, ByVal name As String, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        assert(Not page Is Nothing, "page is nothing.")
        Return invoke(page.inlineScript(), name, actionTarget)
    End Function
End Class